;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Purchase.DealerInOutCase", [])
        .controller("DealerInOutCaseCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                allChecked: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.activeFlag = [false, false];
            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };
            $scope.toolbarConfig = {};
            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.condition = {
                isDealer: !$scope.isZHJCH,
                startDate: new Date(),
                endDate: new Date(),
            };
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1200,
                tableCol: [
                    {
                        type: 'operate',
                        name: "操作",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "所属区域",
                        dataField: "area",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "代理商简称",
                        dataField: "dealerShortName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "代理商名称",
                        dataField: "dealerName",
                        width: 350,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "服务代表",
                        dataField: "serviceRep",
                        width: 200,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "提报月份",
                        dataField: "saleDateShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "任务数",
                        dataField: "taskNum",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    }
                ]
            };

            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("device", "/saletask/exportAll")
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                $scope.markInfo.curRow = -1;
                var service = "device";
                var reqUrl = "/saletask/list";
                
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: angular.copy($scope.condition)
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.saleDateShow = $filter("YbDate")(item.saleDate, "yyyy-MM");
                        item.isChecked = false;
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {
                    isDealer: !$scope.isZHJCH,
                    startDate: new Date(),
                    endDate: new Date(),
                };
            };

            $scope.selectAll = function (list, type) {
                for (var i = 0, len = list.length; i < len; i++) {
                    list[i].isChecked = angular.isUndefined(type) ? $scope.markInfo.allChecked : $scope.markInfo["allChecked" + type];
                }
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item, list, type) {
                var selectAll = false;
                if (!item.isChecked) {
                    selectAll = false;
                } else {
                    if (!checkAll(list)) {
                        selectAll = false;
                    } else {
                        selectAll = true;
                    }
                }
                if (angular.isDefined(type)) {
                    $scope.markInfo["allChecked" + type] = selectAll;
                } else {
                    $scope.markInfo.allChecked = selectAll;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        $scope.detailInfo = {};
                        break;
                    default:
                        break;
                }
            };

            $scope.detailInfo = {};

            $scope.addData = function() {
                $scope.tabFlag.type = 0;
                $scope.tabFlag.name = tabFlag[0];
                $scope.changeTab(1);
            };

            $scope.editData = function(item) {
                $scope.tabFlag.type = 1;
                $scope.tabFlag.name = tabFlag[1];
                $scope.detailInfo = JSON.parse(JSON.stringify(item));
                if ($scope.detailInfo.saleDate) {
                    $scope.detailInfo.saleDate = new Date($scope.detailInfo.saleDate)
                }
                $scope.changeTab(1);
            };

            $scope.deleteData = function(item) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/saletask/delete";
                    ybHttp.Post(service, reqUrl, item).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                })
            };

            $scope.saveData = UserContextService.Debounce(function (argu) {
                saveDataFunc(argu);
            }, 500);

            function saveDataFunc (valid) {
                if (!valid) {
                    $scope.submitted = true;
                    return;
                }
                var url = $scope.tabFlag.type == 0 ? '/saletask/add' : '/saletask/update';
                var param = angular.extend($scope.detailInfo);
                ybHttp.Post('device', url, param).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.search(1);
                    $scope.changeTab(0);
                });
            }

            $scope.selectDealer = function () {
                var param = {
                    name: "代理商",
                    type: "dealer",
                    isPlatCompany: 1
                };
                ybWidget.SingleSelector(param).then(function (data) {
                    $scope.detailInfo.dealerName = data.companyName;
                    $scope.detailInfo.dealerShortName = data.shortName;
                    $scope.detailInfo.dealerOrgId = data.platOrgId;
                    $scope.detailInfo.area = data.area;
                })
            };

            $scope.importData = function () {
                var param = {
                    name: "代理商进销存情况",
                    templateReqService: "device",
                    templateReqUrl: "/saletask/downloadExcel",
                    importReqService: "device",
                    importReqUrl: "/saletask/importExcel",
                };
                ybWidget.ImportFile(param).then(function () {
                    getDataList(1);
                });
            };


            (function () {
                getDataList();
            })();
        }])
    ;
})(window, window.angular);